/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ubbcluj.dbmanagementserver.service;

import com.ubbcluj.dbmanagementserver.model.Database;
import com.ubbcluj.dbmanagementserver.model.Databases;
import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 *
 * @author Bea
 */
public class DBXMLSerializer {

    public static String DBFILENAME = "databases.xml";

    public static List<Database> readDBFromXML() {
        try {
            JAXBContext context = JAXBContext.newInstance(Databases.class);
            Unmarshaller um = context.createUnmarshaller();
            return ((Databases) um.unmarshal(new File(DBFILENAME))).getDatabases();
        } catch (JAXBException ex) {
            Logger.getLogger(DBXMLSerializer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static void saveDBToXML(List<Database> databases) {
        try {
            Databases databases1 = new Databases(databases);
            JAXBContext context = JAXBContext.newInstance(Databases.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(databases1, new File(DBFILENAME));
        } catch (JAXBException ex) {
            Logger.getLogger(DBXMLSerializer.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
